<?php
include_once 'calc_constants.php'; 
$config_file = parse_ini_file("../../properties/config.ini");
$include_path = "../";
include_once("../util/log_writer.php");
$lw = LogWriter::getInstance("../../properties/log4php-level2.properties");
$func = $_POST["func"];
$lw->debug("function = " . $func);
$error = false;
$msg = "";
$result1 = null;
$result2 = null;
$result3 = null;
if ($func == "C") { //CO2
	
} else if ($func == "D") { //Dry dosing
	
} else if ($func == "E") { //Electricity
	
} else if ($func == "H") { //Hardness
	
} else if ($func == "L") { //Liquid dosing
	
} else if ($func == "V") { //volume
	$type = $_POST["tank-type"];
$lw->debug("tank type = " . $type);
	if ($type == "") {
		$error = true;
		$msg = $msg . " Select tank type first.";
	} else {
		$length = " Outer Length";
		$l=null;
		$h=null;
		$w=null;
		$wc=null;
		$t=null;
		$hs=null;
		$unit=null;
		if ($type == "CI") $length = " Outer Diameter";
		else if ($type == "HE" or $type == "OC") $length = " Outer Length of each side";
		//do the validations
	$lw->debug("validating unit");
		if ($_POST["vol-dim-units"] == "") {
			$error = true;
			$msg = $msg . " Dimension Unit is required.";
		} else $unit = $_POST["vol-dim-units"];
	
	$lw->debug("validating L");
		if (trim($_POST["L"]) == "") {
			$error = true;
			$msg = $msg . $length . " is required.";
		} else if (!is_numeric(trim($_POST["L"]))) {
			$error = true;
			$msg = $msg . $length . " should be numeric.";
		} else $l = floatval(trim($_POST["L"]));
	
	$lw->debug("validating H");
		if (trim($_POST["H"]) == "") {
			$error = true;
			$msg = $msg . " Outer Height is required.";
		} else if (!is_numeric(trim($_POST["H"]))) {
			$error = true;
			$msg = $msg . " Outer Height should be numeric.";
		} else $h = floatval(trim($_POST["H"]));
		
		if ($type == "BO" or $type == "CO"or $type == "RE") {
	$lw->debug("validating W");
			if (trim($_POST["W"]) == "") {
				$error = true;
				$msg = $msg . " Outer Width is required.";
			} else if (!is_numeric(trim($_POST["W"]))) {
				$error = true;
				$msg = $msg . " Outer Width should be numeric.";
			} else $w = floatval(trim($_POST["W"]));
		}
	
		if ($type == "BO" or $type == "CO") {
	$lw->debug("validating Wc");
			if (trim($_POST["Wc"]) == "") {
				$error = true;
				$msg = $msg . " Center Width is required.";
			} else if (!is_numeric(trim($_POST["Wc"]))) {
				$error = true;
				$msg = $msg . " Center Width should be numeric.";
			} else $wc = floatval(trim($_POST["Wc"]));
		}
	
	$lw->debug("validating T");
		if (trim($_POST["T"]) == "") {
			$error = true;
			$msg = $msg . " Glass Thickness is required.";
		} else if (!is_numeric(trim($_POST["T"]))) {
			$error = true;
			$msg = $msg . " Glass Thickness should be numeric.";
		} else $t = floatval(trim($_POST["T"]));
	
	$lw->debug("validating Hs");
		if (trim($_POST["Hs"]) != "" and !is_numeric(trim($_POST["Hs"]))) {
			$error = true;
			$msg = $msg . " Average Substrate Depth should be numeric.";
		} else if (trim($_POST["Hs"]) != "") $hs = floatval(trim($_POST["Hs"]));
	$lw->debug("msg=" . $msg);
		if (!$error) {
	$lw->debug("calculating tank vol");
			calculateTankVolume($l,$h,$w,$wc,$t,$hs,$type,$unit,$lw);
	$lw->debug("calculated tank vol");
	$lw->debug("tank vol1=" . $result1);
		}
		switch ($unit) {
			case "C": $converter = $vol_conv_cucm;
						break;
			case "F": $converter = $vol_conv_cuft;
						break;
			case "I": $converter = $vol_conv_cuin;
						break;
			case "M": $converter = $vol_conv_cumm;
		}
	}
	$lw->debug("parsing response");
		include("msg_parser_vol_calc.php");
	$lw->debug("parsed response");
}

function calculateTankVolume($l,$h,$w,$wc,$t,$hs,$type,$unit,$lw) {
$lw->debug("l=" . $l);
$lw->debug("h=" . $h);
$lw->debug("w=" . $w);
$lw->debug("wc=" . $wc);
$lw->debug("t=" . $t);
$lw->debug("hs=" . $hs);
$lw->debug("type=" . $type);
$lw->debug("unit=" . $unit);
	$area = 0;
	switch($type) {
		case "BO": 
					$segment_area = getSegmentArea(($l - (2 * $t))/2, ($wc - $w - (2 * $t)));
					$rectangle = ($l - (2 * $t)) * ($w - (2 * $t));
					$area = $rectangle + $segment_area;
					break;
		case "CI": 
					$area = 0.786 * ($l - (2 * $t)) * ($l - (2 * $t));
					break;
		case "CO": 
					$segment_area = getSegmentArea(($l - (2 * $t))/2, ($w - $wc - (2 * $t)));
					$rectangle = ($l - (2 * $t)) * ($w - (2 * $t));
					$area = $rectangle - $segment_area;
					break;
		case "HE": 
					$area = ($l - (1.155 * $t)) * ($l - (1.155 * $t)) * 2.598;
					break;
		case "OC": 
					$area = ($l - (0.828 * $t)) * ($l - (0.828 * $t)) * 4.828;
					break;
		case "RE": 
					$area = ($l - (2 * $t)) * ($w - (2 * $t));
					break;
		case "SC": 
					$area = 0.393 * ($l - (2 * $t)) * ($l - (2 * $t));
	}
$lw->debug("area=" . $area);
	$GLOBALS["result1"] = $area * ($h - $t);
	if (isset($hs)) {
		$GLOBALS["result2"] = $area * ($hs - $t);
	} else $GLOBALS["result2"] = 0;
$lw->debug("substrate vol=" . $GLOBALS["result2"]);
	$GLOBALS["result3"] = $GLOBALS["result1"] - $GLOBALS["result2"];
$lw->debug("water vol=" . $GLOBALS["result3"]);
}

function getSegmentArea($ae,$ed) {
	$ce = ($ae * $ae)/$ed;
	$ao = ($ed + $ce)/2;
	$oe = $ao - $ed;
	$aob = 2 * atan2($ae,$oe);
	$sector = ($aob * $ao * $ao)/2;
	$triangle = $oe * $ae;
	return ($sector - $triangle);
}
?>
